package com.zcdyy.rentalofhouse.ui.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zcdyy.rentalofhouse.R;
import com.zcdyy.rentalofhouse.application.MyApplication;
import com.zcdyy.rentalofhouse.bean.Account;
import com.zcdyy.rentalofhouse.bean.OrderInfo;
import com.zcdyy.rentalofhouse.bean.ShopRent;
import com.zcdyy.rentalofhouse.constant.Constants;
import com.zcdyy.rentalofhouse.utils.BaseTranslucentActivity;
import com.zcdyy.rentalofhouse.utils.BmobUtils;
import com.zcdyy.rentalofhouse.utils.ImageLoaderUtils;
import com.zcdyy.rentalofhouse.utils.ImageUploadUtil;
import com.zcdyy.rentalofhouse.utils.ImageUtil;
import com.zcdyy.rentalofhouse.utils.ToastUtils;
import com.zcdyy.rentalofhouse.views.ActionSheet;
import com.zcdyy.rentalofhouse.views.CircleImageView;

import org.w3c.dom.Text;

import java.io.File;
import java.io.StringWriter;

import cn.bmob.v3.datatype.BmobFile;

public class ShopsActivity extends BaseTranslucentActivity implements View.OnClickListener,ActionSheet.OnSheetItemClickListener {
    private TextView tvTitle;
    private CircleImageView civ_head;
    private TextView tv_leixing;
    private EditText etQuYu;
    private EditText etDiDuan;
    private EditText etMianJi;
    private EditText etShoujia;
    private RelativeLayout rlLeiXing;
    private EditText etBiaoTi;
    private RelativeLayout rlDesc;
    private TextView tv_miaoshu;
    private EditText etLianXiRen;
    private EditText etShouJiHao;
    private TextView publish;

    public ShopRent shopRent;
    public OrderInfo orderInfo;
    private Account loginInfo;
    private String[] items = {"拍照", "我的相册"};
    // 头像文件、上传头像的名称、本地图片uri
    private File imageFile;
    private Uri imageUri; // 图片路径
    private String saveFileName = "";
    private BmobUtils bmobUtils;
    private static int type = 0;//0表示整租，1表示二手房，2表示单间，3表示商铺
    //拍照、从相册选择、照片保存成功
    private final int TAKE_PHOTO = 1, CHOOSE_PHOTO = 2, SAVE_IMAGE_SUCCESS = 3;
    private final int REQUEST_CODE = 0;
    public Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case Constants.ResultCode.SUCCESS:
                    ImageLoaderUtils.initImage(ShopsActivity.this,shopRent.getPic().getFileUrl(),
                            civ_head,R.drawable.chat_xe_icon2_03);
                    dialog.dismiss();
                    break;
                case Constants.ResultCode.UPLOAD_SUCCESS:
                    dialog.dismiss();
                    ToastUtils.shortToast(ShopsActivity.this,"发布成功");
                    BmobUtils.query = false;
                    BmobUtils.queryCount = 0;
                    startActivity(new Intent(ShopsActivity.this,MainActivity.class));
                    finish();
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loginInfo = MyApplication.getInstance().readLoginUser();
        setContentView(R.layout.activity_shops);
        initView();
        initTitle();
        bind();
    }

    private void bind() {
        bmobUtils = new BmobUtils(this);
        civ_head.setOnClickListener(this);
        rlLeiXing.setOnClickListener(this);
        rlDesc.setOnClickListener(this);
        publish.setOnClickListener(this);
    }

    private void initView() {
        shopRent = new ShopRent();
        orderInfo = new OrderInfo();
        tv_miaoshu = (TextView) findViewById(R.id.tv_miaoshu_choose);
        publish = (TextView) findViewById(R.id.bt_publish);
        tv_leixing = (TextView) findViewById(R.id.tv_pay_fix);
        tvTitle = (TextView) findViewById(R.id.top_tv_title);
        civ_head = (CircleImageView) findViewById(R.id.civ_header);
        etQuYu = (EditText) findViewById(R.id.ed_quyu);
        etDiDuan = (EditText) findViewById(R.id.et_xiaoqu1);
        etMianJi = (EditText) findViewById(R.id.et_ceng);
        etShoujia = (EditText) findViewById(R.id.et_yuan);
        rlLeiXing = (RelativeLayout) findViewById(R.id.rl_pay_sytle);
        etBiaoTi = (EditText) findViewById(R.id.et_xiaoqu);
        rlDesc = (RelativeLayout) findViewById(R.id.rl_desc);
        etLianXiRen = (EditText) findViewById(R.id.et_lianxiren);
        etShouJiHao = (EditText) findViewById(R.id.et_shoujihao);
    }

    private void initTitle() {
        tvTitle.setText("商铺出租");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.civ_header:
                showActionSheetDialog(items);
                break;
            case R.id.rl_pay_sytle:
                Intent intent1 = new Intent(ShopsActivity.this,PayStyleActivity.class);
                intent1.putExtra("type","shop");
                startActivityForResult(intent1,400);
                break;
            case R.id.bt_publish:
                publishRental();
                break;
            case R.id.rl_desc:
                Intent intent = new Intent(ShopsActivity.this,MoreDescActivity.class);
                startActivityForResult(intent,200);
                break;
        }
    }

    private void publishRental() {
        if (check()){

            shopRent.setId(loginInfo.getId());
            shopRent.setBiaoTi(etBiaoTi.getText().toString());
            shopRent.setDiDuan(etDiDuan.getText().toString());
            shopRent.setLeiXing(tv_leixing.getText().toString());
            shopRent.setLianXiRen(etLianXiRen.getText().toString());
            shopRent.setMianJi(etMianJi.getText().toString());
            shopRent.setShouJiHao(etShouJiHao.getText().toString());
            shopRent.setShouJia(etShoujia.getText().toString());
            shopRent.setQuYu(etQuYu.getText().toString());
            shopRent.setMiaoShu(tv_miaoshu.getText().toString());

            orderInfo.setId(loginInfo.getId());
            orderInfo.setType("3");
            orderInfo.setRent_money(etShoujia.getText().toString());
            orderInfo.setCity(etQuYu.getText().toString());
            orderInfo.setTitle(etBiaoTi.getText().toString());


            dialog = ProgressDialog.show(this, null, "正在发布...");
            bmobUtils.uploadShopRent(shopRent,orderInfo);
        }else {
            ToastUtils.shortToast(this,"请填写完整信息哦~~~");
        }
    }

    private boolean check() {
        if (imageFile==null||etQuYu.getText().toString().equals("")||etDiDuan.getText().toString().equals("")
                ||etMianJi.getText().toString().equals("")||etShoujia.getText().toString().equals("")
                ||etBiaoTi.getText().toString().equals("")||tv_miaoshu.getText().toString().equals("")
                ||etLianXiRen.getText().toString().equals("")||etShouJiHao.getText().toString().equals("")){
            return false;
        }else {
            return true;
        }

    }

    //头像选择对话框
    private void showActionSheetDialog(String[] items) {
        ActionSheet actionSheet = new ActionSheet(this)
                .builder()
                .setCancelable(false)
                .setCanceledOnTouchOutside(true);
        for (int i = 0; i < items.length; i++) {
            actionSheet.addSheetItem(items[i], ActionSheet.SheetItemColor.Blue, ShopsActivity.this);
        }
        actionSheet.show();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       // Log.e("steq",requestCode+"--"+data.getStringExtra("payStyle")+"");
        switch (requestCode){
            case 200:
                if (data == null){
                    tv_miaoshu.setHint("房源描述");
                }else {
                    String str = data.getStringExtra("desc");
                    tv_miaoshu.setText(str);
                }

                break;
            case 400:
                if (data != null){
                    String str1 = data.getStringExtra("payStyle");
                    tv_leixing.setText(str1);
                }
            case TAKE_PHOTO:
                if (resultCode == 0) {
                    return;
                }
                //拍照
                ImageUtil.paizhaocreateImagefile(this, imageFile);
                if (imageFile == null) {
                    ToastUtils.shortToast(this, "图片太大无法上传");
                    return;
                }
                doSendThread();//上传图片
                break;
            case CHOOSE_PHOTO:
                //相册
                if (resultCode == 0) return;
                imageUri = data.getData();
                imageFile = MyApplication.getInstance().createimagefile(imageUri, getVmWidth(), getVmHeight());
                if (imageFile == null) {
                    ToastUtils.shortToast(this, "imageFile为空2");
                    return;
                }
                doSendThread();//上传图片
                break;
        }
    }

    private void doSendThread() {
        Log.e("image",imageFile.getPath()+"");
        dialog = ProgressDialog.show(this, null, "正在上传照片...");
//        Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getPath());
//        civ_head.setImageBitmap(bitmap);
        BmobFile bmobFile = new BmobFile(imageFile);
        bmobUtils.upLoadShopFile(bmobFile);
    }

    @Override
    public void onClick(int which) {
        switch (which) {
            case TAKE_PHOTO:
                //拍照
                imageFile = ImageUploadUtil.createImageFile(ImageUploadUtil.CAMERA_SAVEDIR2, ImageUploadUtil.createImageName());
                startActivityForResult(ImageUploadUtil.intentImageCapture(imageFile), ImageUploadUtil.TAKE_PHOTO);
                break;
            case CHOOSE_PHOTO:
                //从相册选择
                startActivityForResult(ImageUtil.intentChooseImg(),
                        CHOOSE_PHOTO);
                break;
        }
    }
}
